// pages/personal/checkCar/contactsMsg/index/index.js
import api from '../../../../../utils/api'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        person: 'https://cdn.oss.dgddaijia.com/icons/personal.png',
        radio: '1',
        inpIndex: '',
        contactData: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    add(e) {
        var type = e.currentTarget.dataset.type
        wx.navigateTo({
            url: '/pages/personal/checkCar/contactsMsg/add/add?type=' + type,
        })
    },
    choseUser(e) {
        var userData = e.currentTarget.dataset.user
        userData = JSON.stringify(userData)
        wx.redirectTo({
            url: '/pages/personal/checkCar/index/index?data=' + userData,
        })
    },
    editAdr(e) {
        var data = e.currentTarget.dataset.data
        var type = e.currentTarget.dataset.type
        data = JSON.stringify(data)
        wx.navigateTo({
            url: '/pages/personal/checkCar/contactsMsg/add/add?data=' + data + '&type=' + type,
        })
    },
    delAdr(e) {
        var id = e.currentTarget.dataset.id
        let apis = api.api;
        let url = apis.host + 'Userdjorder/del_linkuser';
        let data = {
            id: id
        };
        var that = this
        apis.request(url, data, 'POST').then((res) => {
            wx.showToast({
                title: '删除成功',
                icon: 'success',
                success() {
                    that.getData()
                }
            })
        }).catch(function (res) {})
    },
    onChange(e) {
        var data = this.data.contactData
        data.map((item, index) => {
            if (item.id == e.detail) {
                var datas = this.data.contactData[index]
                this.setDefault(datas)
            }
        })
        this.setData({
            radio: e.detail,
            inpIndex: e.detail
        });
    },
    setDefault(nowData) {
        // 修改默认联系人
        let apis = api.api;
        let url = apis.host + 'Userdjorder/linkuser_edit';
        let data = nowData;
        data.is_default = 1
        if (data.car_code == null) {
            data.car_code = ''
        }
        apis.request(url, data, 'POST').then((res) => {
            wx.showToast({
                title: '修改成功',
                icon: 'success'
            })
        }).catch(function (res) {})
    },
    getData() {
        // 获取联系人内容
        let apis = api.api;
        var that = this
        let url = apis.host + 'Userdjorder/get_link_user';
        let data = {};
        apis.request(url, data, 'POST').then((res) => {
            if (res.code == 200) {
                res.data.map((item, index) => {
                    if (item.is_default == '1') {
                        this.data.radio = item.id
                    }
                })
                this.setData({
                    contactData: res.data,
                    radio: this.data.radio
                })
            }
        }).catch(function (res) {
            console.log('订单开始失败', res)
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})